package com.coincalf.wallet.blockchain.client;

import com.coincalf.feign.CoinServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import wf.bitcoin.javabitcoindrpcclient.BitcoinJSONRPCClient;

import javax.annotation.PostConstruct;
import java.net.URL;


/**
 * 获取USDT JAVA客户端
 */
@Slf4j
@Component
public class UsdtClientSingleton {

    @Autowired
    private CoinServiceClient coinServiceClient;

    private static CoinServiceClient coinClient;

    @PostConstruct
    public void init() {
        UsdtClientSingleton.coinClient = coinServiceClient;
    }

    private UsdtClientSingleton() {
    }

    private static volatile BitcoinJSONRPCClient usdtClient = null;

    public static BitcoinJSONRPCClient getUsdtClient() {
        if (usdtClient == null) {
            synchronized (UsdtClientSingleton.class) {
                if (usdtClient == null) {
                    try {
                        usdtClient = new BitcoinJSONRPCClient(new URL(coinClient.selectRpcServerUrl("USDT_OMNI")));
                    } catch (Exception e) {
                        log.warn("获取USDT CLIENT ERROR e={}", e);
                        e.printStackTrace();
                    }
                }
            }
        }
        return usdtClient;
    }

}
